package com.tx.core.generator.model.impl;

import org.apache.commons.lang3.StringUtils;

import com.tx.core.generator.model.EntityProperty;
import com.tx.core.generator.util.JPAGeneratorUtils;
import com.tx.core.util.TableInfoUtils;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.media.Schema;

/**
 * <pre>
 * 功能简述: JPAEntityProperty
 * </pre>
 *
 * @author PengQingyang
 * 创建时间 [2024/4/2]
 */
public class JPAEntityProperty implements EntityProperty {

    /** jpaColumn的信息 */
    private final TableInfoUtils.TableColumnInfo jpaColumnInfo;

    /** 验证表达式 */
    private String validateExpression;
    
    private String columnType;

    public String getColumnType() {
		return columnType;
	}

	public void setColumnType(String columnType) {
		this.columnType = columnType;
	}

	/** <默认构造函数> */
    public JPAEntityProperty(TableInfoUtils.TableColumnInfo column) {
        this.jpaColumnInfo = column;
    }

    /**
     * @return 返回 validateExpression
     */
    @Override
    public String getValidateExpression() {
        return validateExpression;
    }

    /**
     * @param validateExpression
     */
    @Override
    public void setValidateExpression(String validateExpression) {
        this.validateExpression = validateExpression;
    }

    /**
     * @return
     */
    @Override
    public boolean isEnum() {
        return this.jpaColumnInfo.getPropertyType().isEnum();
    }

    /**
     * @return
     */
    @Override
    public boolean isBasicdataEnum() {
        return JPAGeneratorUtils.isBasicdataEnum(this.jpaColumnInfo.getPropertyType());
    }

    /**
     * @return
     */
    @Override
    public boolean isBasicdataType() {
        return JPAGeneratorUtils.isBasicdataType(this.jpaColumnInfo.getPropertyType());
    }

    /**
     * @return 返回 jpaColumnInfo
     */
    public TableInfoUtils.TableColumnInfo getJpaColumnInfo() {
        return jpaColumnInfo;
    }

    /**
     * @return
     */
    @Override
    public String getPropertyReadMethodName() {
        return this.jpaColumnInfo.getPropertyDescriptor()
                .getReadMethod()
                .getName();
    }

    /**
     * @return
     */
    @Override
    public String getPropertyTypeName() {
        return this.jpaColumnInfo.getPropertyType().getName();
    }

    /**
     * @return
     */
    @Override
    public String getPropertyTypeSimpleName() {
        return this.jpaColumnInfo.getPropertyType().getSimpleName();
    }

    /**
     * @return
     */
    @Override
    public boolean isPrimaryKey() {
        return this.jpaColumnInfo.isPrimaryKey();
    }

    /**
     * @return
     */
    @Override
    public String getPropertyName() {
        return this.jpaColumnInfo.getPropertyName();
    }

    /**
     * @return
     */
    @Override
    public boolean isSimpleValueType() {
        return this.jpaColumnInfo.isSimpleValueType();
    }

    @Override
    public boolean isPrimitiveType() {
        return this.jpaColumnInfo.getPropertyType().isPrimitive();
    }

    /**
     * @return
     */
    @Override
    public String getColumnName() {
        return this.jpaColumnInfo.getColumnName();
    }

    /**
     * @return
     */
    @Override
    public String getColumnPropertyName() {
        return this.jpaColumnInfo.getColumnPropertyName();
    }

    /**
     * @return
     */
    @Override
    public String getColumnPropertyTypeName() {
        return this.jpaColumnInfo.getColumnPropertyType().getName();
    }

    /**
     * @return
     */
    @Override
    public String getColumnPropertyTypeSimpleName() {
        return this.jpaColumnInfo.getColumnPropertyType().getSimpleName();
    }

    /**
     * @return
     */
    @Override
    public boolean isNullable() {
        return this.jpaColumnInfo.isNullable();
    }

    /**
     * @return
     */
    @Override
    public int getLength() {
        return this.jpaColumnInfo.getLength();
    }

    /**
     * @return
     */
    @Override
    public int getPrecision() {
        return this.jpaColumnInfo.getPrecision();
    }

    /**
     * @return
     */
    @Override
    public int getScale() {
        return this.jpaColumnInfo.getScale();
    }

    /**
     * @return
     */
    @Override
    public boolean isUnique() {
        return this.jpaColumnInfo.isUnique();
    }

    /**
     * @return
     */
    @Override
    public boolean isInsertable() {
        return this.jpaColumnInfo.isInsertable();
    }

    /**
     * @return
     */
    @Override
    public boolean isUpdatable() {
        return this.jpaColumnInfo.isUpdatable();
    }

    /**
     * 获取字段描述<br/>
     * <功能详细描述>
     * @return [参数说明]
     *
     * @return String [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    @Override
    public String getPropertyComment() {
        String propertyComment = getPropertyName();
        if (this.jpaColumnInfo.getTypeDescriptor()
                .hasAnnotation(ApiModelProperty.class)) {
            ApiModelProperty anno = this.jpaColumnInfo.getTypeDescriptor()
                    .getAnnotation(ApiModelProperty.class);
            if (StringUtils.isNotBlank(anno.value())) {
                propertyComment = anno.value();
                return propertyComment;
            }
        }
        if (this.jpaColumnInfo.getTypeDescriptor()
                .hasAnnotation(Schema.class)) {
            Schema scAnno = this.jpaColumnInfo.getTypeDescriptor()
                    .getAnnotation(Schema.class);
            if (StringUtils.isNotBlank(scAnno.description())) {
                propertyComment = scAnno.description();
                return propertyComment;
            }
        }
        switch (propertyComment) {
            case "id":
                propertyComment = "ID";
                break;
            case "parentId":
                propertyComment = "上级ID";
                break;
            case "code":
                propertyComment = "编码";
                break;
            case "tenant":
                propertyComment = "租户";
                break;
            case "tenantId":
                propertyComment = "租户ID";
                break;
            case "vcid":
                propertyComment = "虚中心ID";
                break;
            case "organization":
                propertyComment = "组织";
                break;
            case "organizationId":
                propertyComment = "组织机构ID";
                break;
            case "company":
                propertyComment = "公司";
                break;
            case "companyId":
                propertyComment = "公司ID";
                break;
            case "type":
                propertyComment = "类型";
                break;
            case "typeId":
                propertyComment = "类型ID";
                break;
            case "category":
                propertyComment = "分类";
                break;
            case "categoryId":
                propertyComment = "分类ID";
                break;
            case "catalog":
                propertyComment = "目录";
                break;
            case "catalogId":
                propertyComment = "目录ID";
                break;
            case "name":
                propertyComment = "名称";
                break;
            case "value":
                propertyComment = "值";
                break;
            case "valid":
                propertyComment = "是否有效";
                break;
            case "modifiable":
                propertyComment = "是否可编辑";
                break;
            case "modifyAble":
                propertyComment = "是否可编辑";
                break;
            case "remark":
                propertyComment = "备注";
                break;
            case "description":
                propertyComment = "描述";
                break;
            case "createDate":
                propertyComment = "创建时间";
                break;
            case "createOperatorId":
                propertyComment = "创建人ID";
                break;
            case "lastUpdateDate":
                propertyComment = "最后更新时间";
                break;
            case "lastUpdateTime":
                propertyComment = "最后更新时间";
                break;
            case "updateDate":
                propertyComment = "最后更新时间";
                break;
            case "updateTime":
                propertyComment = "最后更新时间";
                break;
            case "lastUpdateOperatorId":
                propertyComment = "最后更新人ID";
                break;
            case "lastUpdateUser":
                propertyComment = "最后更新人";
                break;
            case "updateOperatorId":
                propertyComment = "最后更新人ID";
                break;
            case "updateUserId":
                propertyComment = "最后更新人ID";
                break;
            case "updateUser":
                propertyComment = "最后更新人";
                break;
            case "effictiveDate":
                propertyComment = "生效时间";
                break;
            case "expiryDate":
                propertyComment = "到期时间";
                break;
        }
        return propertyComment;
    }


}
